/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:itsm-gateway
 * Create Time: 2016年1月30日 下午3:48:15
 */
package com.lenovo.lmrp.server.itsmgateway.repository;

import java.util.List;
import java.util.Objects;

import javax.inject.Named;

import com.lenovo.lmrp.api.model.entity.EntityMetadataPO;
import com.lenovo.lmrp.api.model.entity.FieldMetadataPO;
import com.lenovo.lmrp.api.model.entity.TicketSystemPO;
import com.lenovo.lmrp.server.itsmgateway.domain.metadata.ITicketSystemRepository;
import com.lenovo.lmrp.server.itsmgateway.domain.metadata.ItsmEntityTranslator.EntityName;
import com.lenovo.lmrp.server.itsmgateway.domain.metadata.TicketSystemBO;
import com.lenovo.sdc.framework.annotation.DomainRepository;
import com.lenovo.sdc.framework.dao.ADaoFactory;
import com.lenovo.sdc.framework.dao.ICommonDAO;
import com.lenovo.sdc.framework.dao.RecordFilter;
import com.lenovo.sdc.framework.util.Condition;
import com.lenovo.sdc.framework.util.exception.ErrorCode;
import com.lenovo.sdc.framework.util.exception.TraceableException;

/**
 * 
 * @author wujin
 * @version v1.0
 */
@Named
@DomainRepository
public class TicketSystemRepository implements ITicketSystemRepository {

    @Override
    public TicketSystemBO getTicketSystem(String itsmCode) {
        TicketSystemPO po = null;
        try (ICommonDAO dao = ADaoFactory.getDefault().createDAO()) {
            po = dao.get(TicketSystemPO.class, itsmCode);
        }
        catch (Exception exp) {
            throw new TraceableException(ErrorCode.DB_QUERY_FAILED, exp.getMessage());
        }
        
        // wrap TicketPO to TicketBO
        if (Objects.isNull(po))
            throw new TraceableException(ErrorCode.DB_RECORD_NOTFOUND, "TicketSystem:" + itsmCode);
        else
            return new TicketSystemBO(po);
    }

    public EntityMetadataPO getEntityMetadata(String itsmCode, EntityName entityName) {
        // need to cache EntityMetadataPO instance because it is rarely changed and read more
        // read cache first if not found load from DB then put to cache
        try (ICommonDAO dao = ADaoFactory.getDefault().createDAO()) {
            RecordFilter filter = new RecordFilter();
            // mysql ignore case considerations
            filter.addCondition(new Condition<String, Object>("itsmcode", itsmCode));
            filter.addCondition(new Condition<String, Object>("physicalname", entityName.name()));
            List<EntityMetadataPO> entityMetadata = dao.filteByCondition(EntityMetadataPO.class, filter);

            if (entityMetadata.isEmpty())
                throw new TraceableException(ErrorCode.DB_RECORD_NOTFOUND, entityName.name() + " isn't defined");

            // itsmcode + physicalname is unique index, so only need get one record
            filter.clean();
            filter.addCondition(new Condition<String, Object>("entityid", entityMetadata.get(0).getId()));
            List<FieldMetadataPO> fieldMetadata = dao.filteByCondition(FieldMetadataPO.class, filter);
            if (fieldMetadata.isEmpty())
                throw new TraceableException(ErrorCode.DB_RECORD_NOTFOUND, entityName.name()
                                                                           + " fields mapping aren't defined");
            filter.clean();
            entityMetadata.get(0).setFieldMetadataList(fieldMetadata);

            return entityMetadata.remove(0);
        }
        catch (Exception exp) {
            throw new TraceableException(ErrorCode.DB_QUERY_FAILED, "load entity metadata failed - " + entityName.name());
        }
    }
}
